using System;
using System.Collections.Generic;

using log4net;

using GymManager.Exception;
using GymManager.Mechanism;
using GymManager.Persistence.DAO;
using GymManager.Persistence.VO;

namespace GymManager.Business
{
    /// <summary>
    /// Class responsible by providing services regarding modality plan
    /// </summary>
    public class ModalityPlanServices
    {
        // logger instance
        private ILog _logManager = LogManager.GetLogger(typeof(ModalityPlanServices));

        /// <summary>
        /// Method responsible by getting all modality plans beloging to an specific modality
        /// </summary>
        /// <param name="Int32">modality identifier</param>
        /// <returns>IList - list of modality plans belonging to an specific modality</returns>
        /// <see cref="GymManager.Exception.BusinessException"/>
        public IList<ModalityPlan> findByModality(Int32 modalityId)
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }

            IList<ModalityPlan> modalityPlanList = null;

            try
            {
                // getting all modality plans belonging to an specific modality
                ModalityPlanDAO modalityPlanDAO = new ModalityPlanDAO();
                modalityPlanList = modalityPlanDAO.findByModality(modalityId);
            }
            catch (PersistenceException)
            {
                throw new BusinessException();
            }            
            
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }

            return modalityPlanList;
        }

        /// <summary>
        /// Method responsible by finding a modality plan by its composed key 
        /// </summary>
        /// <param name="Int32">frequency (1-5)</param>
        /// <param name="PaymentPlan">payment plan</param>
        /// <param name="Int32">modality identifier</param>
        /// <returns>ModalityPlan - modality plans in according to key</returns>
        /// <see cref="GymManager.Exception.BusinessException"/>
        public ModalityPlan findByKey(Int32 frequency, PaymentPlan paymentPlan, Int32 modalityId)
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }

            ModalityPlan modalityPlanVO = null;

            try
            {
                // get a modality plan in according to key
                ModalityPlanDAO modalityPlanDAO = new ModalityPlanDAO();
                modalityPlanVO = modalityPlanDAO.findByKey(frequency, paymentPlan, modalityId);
            }
            catch (PersistenceException)
            {
                throw new BusinessException();
            }

            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }

            return modalityPlanVO;
        }

        /// <summary>
        /// Method responsible by checking if a modality plan is in use by somebody
        /// </summary>
        /// <param name="Int32">modality plan identifier</param>
        /// <returns>Boolean - flag indicating if some modality plan is in use or not</returns>
        /// <see cref="GymManager.Exception.BusinessException"/>
        public Boolean isModalityPlanInUse(Int32 modalityPlanId)
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }

            Boolean isModalityPlanInUse = false;

            try
            {
                // get a modality plan in according to key                                
                ModalityPlanDAO modalityPlanDAO = new ModalityPlanDAO();
                Int32 numberOfStudentsUsingModalityPlan = modalityPlanDAO.getNumberOfStudentsUsingModalityPlan(modalityPlanId);

                // setting the answer
                isModalityPlanInUse = (numberOfStudentsUsingModalityPlan > 0);
            }
            catch (PersistenceException)
            {
                throw new BusinessException();
            }

            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }

            return isModalityPlanInUse;
        }

    }
}
